﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace HoEnglishInput
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private int line_length;
        private int index;
        private string input_text;
        private string text;
        private FlowDocument flow;

        public MainWindow()
        {
            InitializeComponent();

            this.line_length = "AAAAAAAAAAAAAAAAAAAAAAA".Length;
            this.input_text = "";
            this.index = 0;
            this.flow = new FlowDocument();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            // 生成随机字母字符串
            this.text = "";
            Random r = new Random();
            for (int i = 0; i < 8; ++i)
            {
                for (int j = 0; j < this.line_length; ++j)
                {
                    this.text += (char)r.Next('A', 'Z' - 1);
                }
            }

            // 将随机字母追加到TargetTextBx中
            Paragraph p = new Paragraph();
            Run run = new Run(this.text);
            run.Foreground = new SolidColorBrush(Color.FromRgb(200, 200, 200));
            p.Inlines.Add(run);
            this.flow.Blocks.Add(p);
            this.TargetTextBox.Document = this.flow;

            // 默认InputTextBox选中
            this.InputTextBox.Focus();

            // 设置TargetTextBox的属性，防止作弊
            this.TargetTextBox.IsReadOnly = true;
            this.TargetTextBox.Focusable = false;
        }

        private void InputTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            string current_text = this.text[this.index].ToString();
            if (this.InputTextBox.Text == current_text)
            {
                this.InputTextBox.Text = "";
                this.input_text += current_text;
                ++this.index;
                this.setForegroundText();
                if (this.text.Length == this.input_text.Length)
                {
                    MessageBoxResult res = MessageBox.Show("恭喜您已完成录入练习，是否重置？", "系统提示", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.Yes);
                    if (res == MessageBoxResult.Yes)
                    {
                        // 生成随机字母字符串
                        this.text = "";
                        Random r = new Random();
                        for (int i = 0; i < 8; ++i)
                        {
                            for (int j = 0; j < this.line_length; ++j)
                            {
                                this.text += (char)r.Next('A', 'Z' - 1);
                            }
                        }

                        // 将随机字母追加到TargetTextBx中
                        this.flow.Blocks.Clear();
                        Paragraph p = new Paragraph();
                        Run run = new Run(this.text);
                        run.Foreground = new SolidColorBrush(Color.FromRgb(200, 200, 200));
                        p.Inlines.Add(run);
                        this.flow.Blocks.Add(p);
                        this.TargetTextBox.Document = this.flow;
                    }
                }
            }
        }

        private void setForegroundText()
        {
            this.flow.Blocks.Clear();
            Paragraph p = new Paragraph();
            Run run1 = new Run(this.input_text);
            Run run2 = new Run(this.text.Substring(this.input_text.Length));
            run1.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));
            run2.Foreground = new SolidColorBrush(Color.FromRgb(200, 200, 200));
            p.Inlines.Add(run1);
            p.Inlines.Add(run2);
            this.flow.Blocks.Add(p);
            this.TargetTextBox.Document = this.flow;
        }
    }
}
